use arboard::Clipboard;
use std::env::args;
use std::io::{self, Read};

fn echo_to_x() {
    let mut clipboard = Clipboard::new().unwrap();

    if atty::is(atty::Stream::Stdin) {
        if args().len() > 1 {
            // let _ = args().nth(1).unwrap();
            println!("Usage:\n\t\necho 'something' | x ");
            println!("then run x to get something output to your terminal");
            std::process::exit(0);
        }
        // else args len < 1
        let contents = clipboard.get_text().unwrap();
        println!("{}", contents);
    } else {
        // not stdin
        let mut buf = String::new();
        _ = io::stdin().read_to_string(&mut buf);

        clipboard.set_text(buf.to_owned()).unwrap();
        assert_eq!(clipboard.get_text().unwrap(), buf);
    }
}

fn main() {
    echo_to_x();
}
